package com.qk.management.service.impl;

import com.qk.common.PageResult;
import com.qk.entity.Dept;
import com.qk.management.mapper.DeptMapper;
import com.qk.management.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 部门业务层实现类
 *
 * @Author:Mr.m
 * @Date: 2025-09-29 10:56
 * @Description: TODO
 */
@Service
public class DeptServiceImpl implements DeptService {
    @Autowired
    private DeptMapper deptMapper;

    //查询所有部门
    @Override
    public List<Dept> list(){
        return deptMapper.selectAll();
    }

    // 删除部门
    @Override
    public void deleteById(Integer id) {
        deptMapper.deleteById(id);
    }

    /*
    * 更新部门
    * */
    @Override
    public void update(Dept dept) {
        // 设置更新时间
        dept.setUpdateTime(LocalDateTime.now());
        deptMapper.update(dept);
    }


    //  spring 经典面试题  循环依赖问题
//    @Autowired
//    private DeptService deptService;
    /*
    * 根据id查询部门
    * */
    @Override
    public Dept queryDeptById(Integer id) {
        Dept dept = deptMapper.queryDeptById(id);
        return dept;
    }

    /*
     * 根据页面条件查询部门
     * */
    @Override
    public PageResult<Dept> page(String name, Integer status, Integer page, Integer pageSize) {
        //1.查询满足条件的总记录数
        Integer total = deptMapper.count(name, status);
        //2.查询满足条件的当前页数据
        //计算偏移量
        Integer offset = (page - 1) * pageSize;
        List<Dept> rows = deptMapper.selectByPage(name, status, offset, pageSize);
        //3.数据组装
        return PageResult.<Dept>builder()
                .total(total)
                .rows(rows)
                .build();

    }

    /*
     * 新增一个部门
     * */
    @Override
    public void addDept(Dept dept) {
        dept.setCreateTime(LocalDateTime.now());
        dept.setUpdateTime(LocalDateTime.now());
        deptMapper.insert(dept);

    }
}
